home *** CD-ROM | disk | FTP | other *** search
- // VoxelScape using ray-casting
- // Rex Deathstar, June 1995
- // email : deathstr@singnet.com.sg
- // BBS HQ : Developer's Site BBS (065-561-0237)
- // IRC : #coders (nick : DeathScar)
-
- // Snowman : get well soon!
-
- // Yes, source codes source codes! Dun u love it?
- // Sorry this program was never finished, but the idea is there anyway.
- // If you wanna code this on a Pentium, dun use the unrolled loops like
- // I did in this source, it's a bad idea ;)
- // I hope you put in the effort to at least understand what the code does
- // before you use for it for any form, this would be a nice form of
- // appreciation. Also, a greet in your demo would be nice too.
-
- // To all international demo freaks,
- // the following are the local demo groups in Singapore :
- // .Mode XiX
- // .MysTiCal
- // .PowerSurge
- // .Constellation
- // .ArchAngels
- // .BlackMagic
- // .SDF (Salient Demo Force)
- // .WaterLogic
- //
- // Yes, there's a demo scene as far out as in Asia, in Singapore! :)
- //
- // Come and attend our second demo party in Singapore, called 'The Scene 96'!
- // U can get the info file from Developer's Site BBS (065-561-0237)
- //
- // WaterLogic productions
- //===========================================================================
- //............................Year 1994......................................
- //1. REXINTRO.ZIP....debut selftro
-
- //2. ASYLUM!.ZIP ....Advert for Asylum BBS (closed down now :( )
-
- //3. ICHIBAN!.ZIP....Advert for IchiBan BBS
-
- //4. ANARCHY!.ZIP....Advert for Anarchy Online BBS
-
- //5. ICHIBAN2.ZIP....2nd Advert for IchiBan BBS
-
- //6. COROM.ZIP ....Advert for COROM PRODUCTIONS BBS
-
- //...........................Year 1995.......................................
- //7. _FACES.ZIP ....Slideshow demo featuring realtime crossfading
-
- //8. TINIFIRE.ZIP....76 byte fire routine, real small huh? (Full sources)
-
- //9. TINYSTAR.ZIP....123 byte 3D starfield, another small one. (Full sources)
-
- //10. DELAYDOT.ZIP....3D object morph with delaydots. (Full sources)
-
- //11. PARTICLE.ZIP....3D Lissajous figures morph. (Full sources)
-
- //12. PURENESS.ZIP....1st place megademo during 'The Scene 95' demo party at
- // Seaview Hotel/Singapore on 2nd July 1995.
-
- //13. COROMSRC.ZIP....Source codes to a BBS advert
-
- //14. WATERFAL.ZIP....Source codes to a waterfall effect as seen in REXINTRO.
-
- //............................Year 1996......................................
- //15. PLASWARP.ZIP....Source codes to the plasma and image warp effect as
- // seen in Pureness (needs PURENESS.DAT to run)
-
- //16. DEVSITE!.ZIP....BBS advert for Developer's Site BBS, now WaterLogic's
- // HQ. Features SVGA 3D motion blur.
-
- //16. AGEN-ART.ZIP....High-res JPGs of Agen's art seen in Pureness
-
- //17. STARGATE.ZIP....Source codes to the texture-mapped wormhole seen
- // in Pureness.
-
- //18. MODELIST.ZIP....Generic VESA graphic mode lister with sources in C
-
- //19. VOXELSPC.ZIP....Source codes to a fast height&color interpolated
- // Voxel landscape routine
-
- //20. MPHONG.ZIP......Full sources to transparent motion-blur phong effect
-
- //21. VOXELSP2.ZIP....Full sources to another method of voxel landscape using
- // raycasting
-
- //22. D???????.ZIP....Megademo for 'The Scene 96' using the demo system EXP24
- // Will be released on 2nd June 1996.
-
-
- #pragma inline
- asm .486
-
- #include <math.h>
- #include <dos.h>
- #include <process.h>
- #include <conio.h>
- #include <stdio.h>
- #include "fader.h"
-
-
- float USER_DISTANCE= 80.0;
- float USER_HEIGHT = 100.0;
- float SKY_HEIGHT = 2000.0;
-
- #define rad 0.01745329
- #define VIEWPORT_LEFT -100
- #define VIEWPORT_RITE 100
- #define VIEWPORT_TOP -100
- #define VIEWPORT_BOT 100
- #define VIEWPORT_CENTER 0
- #define SCREEN_WIDTH 320
- #define SCREEN_HEIGHT 200
-
-
- unsigned int sky,ground,colormap,vbuffer,vram=0xa000;
- char pal[256][3];
- float user_angle,ray_angle_left,ray_angle_rite,ray_length;
- long tex_x,tex_y,tex_xadd,tex_yadd;
- long tex_xstart,tex_xend,tex_ystart,tex_yend;
- long user_x,user_y,nframes;
- int ray_x,ray_y,a,key,scanline;
- int udistance=USER_DISTANCE,distance,nap;
- unsigned long y_offset[1000];
- unsigned int hbuffer[320];
-
-
- asm sky_file db 'height.dat',0
- asm ground_file db 'height.dat',0
- asm colormap_file db 'color.dat',0
-
- void graphics();
- void text();
- void load_texture();
- void floor_mapping();
- void sky_mapping();
- void pixel(int x,int y,char tx,char ty);
- void raycast_one_scanline();
- void voxel_one_scanline();
- void vretrace();
- void earth_napping();
-
- void main()
- {
- if(allocmem(4096,&sky)!=-1) exit(1);
- if(allocmem(4096,&ground)!=-1) exit(1);
- if(allocmem(4096,&colormap)!=-1) exit(1);
- if(allocmem(4096,&vbuffer)!=-1) exit(1);
- load_texture();
-
- graphics();
-
- while(!kbhit())
- {
- vretrace();
- sky_mapping();
- floor_mapping();
- nframes++;
- }
-
- getch();
- text();
- }
- //--------------
- void floor_mapping()
- {
-
- asm mov fs,[ground]
- asm mov gs,[colormap]
- asm mov es,[vram]
-
- user_angle+=0.005;
- user_x+=200;
- user_y+=160;
-
- //USER_HEIGHT=200.0*sin(nframes*rad)+200+50;
-
- ray_angle_left=atan(VIEWPORT_LEFT/USER_DISTANCE);
- ray_angle_rite=atan(VIEWPORT_RITE/USER_DISTANCE);
-
- earth_napping();
-
- for(ray_y=20; ray_y<100; ray_y+=1)
- {
- ray_length=(float)USER_DISTANCE/(ray_y+1)*USER_HEIGHT;
- distance=ray_length+800;
-
- tex_xstart=ray_length*sin(ray_angle_left+user_angle)*120;
- tex_ystart=ray_length*cos(ray_angle_left+user_angle)*120;
-
- tex_xend=ray_length*sin(ray_angle_rite+user_angle)*120;
- tex_yend=ray_length*cos(ray_angle_rite+user_angle)*120;
-
- tex_xadd=(tex_xend-tex_xstart)/160;
- tex_yadd=(tex_yend-tex_ystart)/160;
-
- tex_x=tex_xstart+user_x;
- tex_y=tex_ystart+user_y;
-
-
- voxel_one_scanline();
- }
-
-
- asm push ds
- asm pop es
- asm lea di,[hbuffer]
- asm mov cx,320
- asm xor ax,ax
- asm rep stosw
-
- }
- //--------------
- void earth_napping()
- {
- ray_y=100;
-
- ray_length=(float)USER_DISTANCE/(ray_y+1)*USER_HEIGHT;
- distance=ray_length+500;
-
- tex_xstart=ray_length*sin(user_angle)*170;
- tex_ystart=ray_length*cos(user_angle)*170;
-
- tex_x=tex_xstart+user_x;
- tex_y=tex_ystart+user_y;
-
- asm mov bl,byte ptr [tex_x+1]
- asm mov bh,byte ptr [tex_y+1]
- asm movzx ax,byte ptr fs:[bx]
- asm mov cx,80
- asm mul cx
- asm div [distance]
- asm shr ax,2
- asm mov [nap],ax
-
- }
- //--------------
- void voxel_one_scanline()
- {
- asm mov di,[ray_y]
- asm add di,100
- asm shl di,2
- asm mov edi,dword ptr y_offset[di]
- asm mov [scanline],di
-
- asm mov eax,dword ptr [tex_xadd]
- asm mov ebp,dword ptr [tex_yadd]
- asm mov ecx,dword ptr [tex_x]
- asm mov edx,dword ptr [tex_y]
-
- asm lea si,[hbuffer]
-
- asm mov [a],160
- oloop:
- asm mov bl,ch
- asm mov bh,dh
- asm add cx,ax
- asm add dx,bp
-
- asm pusha
- asm mov cl,gs:[bx] // get color
- asm mov ch,cl
-
- asm movzx ax,byte ptr fs:[bx] // get height
- asm mul [udistance] // y'=user.dist*height/total.dist
- asm div [distance]
- //asm mov dx,[si]
- //asm sub dx,ax
- //asm mov [si],ax
- //asm jc done
-
- asm mov bp,ax
- asm shl bp,2
- asm mov eax,dword ptr y_offset[bp]
- asm sub edi,eax
- asm cmp edi,64000
- asm ja done
-
- vstrip:
- asm mov es:[di],cx
- asm mov es:[di+320],cx
- //asm add di,320
- //asm cmp di,[scanline]
- //asm jbe vstrip
- //asm dec dx
- //asm jnz vstrip
-
- done:
- asm popa
- asm add di,2
- asm add si,2
- asm dec [a]
- asm jnz oloop
- }
-
-
- //--------------
- void sky_mapping()
- {
-
- asm mov fs,[sky]
-
- for(ray_y=VIEWPORT_TOP; ray_y<VIEWPORT_CENTER; ray_y++)
- {
- ray_length=(float)USER_DISTANCE/(ray_y-1)*(USER_HEIGHT-SKY_HEIGHT);
-
- tex_xstart=ray_length*sin(ray_angle_left+user_angle)*10;
- tex_ystart=ray_length*cos(ray_angle_left+user_angle)*10;
-
- tex_xend=ray_length*sin(ray_angle_rite+user_angle)*10;
- tex_yend=ray_length*cos(ray_angle_rite+user_angle)*10;
-
- tex_xadd=((tex_xend-tex_xstart)/SCREEN_WIDTH);
- tex_yadd=((tex_yend-tex_ystart)/SCREEN_WIDTH);
-
- tex_x=tex_xstart+(user_x>>5);
- tex_y=tex_ystart+(user_y>>5);
-
- raycast_one_scanline();
- }
-
-
- }
- //--------------
- void raycast_one_scanline()
- {
- asm mov di,[ray_y]
- asm add di,100 //screen center
- asm shl di,2
- asm mov di,y_offset[di] //start offset of current scanline
-
- asm mov eax,dword ptr [tex_xadd]
- asm mov ebp,dword ptr [tex_yadd]
- asm mov ecx,dword ptr [tex_x]
- asm mov edx,dword ptr [tex_y]
-
- asm push ds
- asm mov ds,[vram]
-
- asm rept 320
- asm mov bl,ch
- asm mov bh,dh
- asm add cx,ax
- asm add dx,bp
- asm mov bl,fs:[bx]
- asm mov [di],bl
- asm inc di
- asm endm
-
- asm pop ds
- }
- //--------------
- void graphics()
- {
- FILE *f;
-
- //set mode 13h
- asm mov ax,0x13
- asm int 0x10
-
- // change refresh from 70Hz to 60Hz
- asm cli
- asm mov dx,0x3cc
- asm in al,dx
- asm or al,0xc0 //on bit 7,8 for 480 scan lines
- asm mov dx,0x3c2
- //asm out dx,al
- asm sti
-
-
- f=fopen("raycast.pal","rb");
- fread(pal,1,768,f);
- fclose(f);
-
- VGADAC(&pal[0][0]);
-
-
- for(a=0; a<1000; a++)
- y_offset[a]=320L*a;
- }
-
- //-----------------
- void text()
- {
- asm mov ax,0x03
- asm int 0x10
- }
-
-
- //-----------------
- void load_texture()
- {
- //load sky texture
- asm mov ax,0x3d00
- asm lea dx,[sky_file]
- asm int 0x21
- asm mov bp,ax
-
- asm mov bx,bp
- asm mov ax,0x3f00
- asm xor dx,dx
- asm mov cx,32768
- asm push ds
- asm mov ds,[sky]
- asm int 0x21
- asm pop ds
-
- asm mov bx,bp
- asm mov ax,0x3f00
- asm mov dx,32768
- asm mov cx,32768
- asm push ds
- asm mov ds,[sky]
- asm int 0x21
- asm pop ds
-
- asm mov bx,bp
- asm mov ax,0x3e00
- asm int 0x21
-
- //load ground texture
- asm mov ax,0x3d00
- asm lea dx,[ground_file]
- asm int 0x21
- asm mov bp,ax
-
- asm mov bx,bp
- asm mov ax,0x3f00
- asm xor dx,dx
- asm mov cx,32768
- asm push ds
- asm mov ds,[ground]
- asm int 0x21
- asm pop ds
-
- asm mov bx,bp
- asm mov ax,0x3f00
- asm mov dx,32768
- asm mov cx,32768
- asm push ds
- asm mov ds,[ground]
- asm int 0x21
- asm pop ds
-
- asm mov bx,bp
- asm mov ax,0x3e00
- asm int 0x21
-
- //load color map for ground texture
- asm mov ax,0x3d00
- asm lea dx,[colormap_file]
- asm int 0x21
- asm mov bp,ax
-
- asm mov bx,bp
- asm mov ax,0x3f00
- asm xor dx,dx
- asm mov cx,32768
- asm push ds
- asm mov ds,[colormap]
- asm int 0x21
- asm pop ds
-
- asm mov bx,bp
- asm mov ax,0x3f00
- asm mov dx,32768
- asm mov cx,32768
- asm push ds
- asm mov ds,[colormap]
- asm int 0x21
- asm pop ds
-
- asm mov bx,bp
- asm mov ax,0x3e00
- asm int 0x21
-
-
- // post process textures
- asm mov es,[sky]
- asm xor di,di
- p1:
- asm shr byte ptr es:[di],1
- asm add byte ptr es:[di],128
- asm inc di
- asm jnz p1
-
- asm mov es,[colormap]
- asm xor di,di
- p2:
- asm shl byte ptr es:[di],1
- asm inc di
- asm jnz p2
- }
- //---------------
- void vretrace()
- {
- asm mov dx,0x3da
- vr1:
- asm in al,dx
- asm test al,8
- asm jnz vr1
- vr2:
- asm in al,dx
- asm test al,8
- asm jz vr2
- }
-